package cn.wolfcode.web.controller;


import cn.wolfcode.domain.*;
import cn.wolfcode.qo.ExamQueryObject;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;


    @RequiredPermission(name = "试卷页面", expression = "exam:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Exam> pageInfo = examService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "exam/list";
    }


    @RequiredPermission(name = "试卷删除", expression = "exam:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            examService.delete(id);
        }
        return "redirect:/exam/list";
    }


    @RequiredPermission(name = "试卷预览", expression = "exam:preview")
    @RequestMapping("/preview")
    public String preview(Long id, Model model) {

        //创建单选多选判断题的集合
        ArrayList<Question> onlyList = new ArrayList<>();
        ArrayList<Question> manyList = new ArrayList<>();
        ArrayList<Question> trueOrFalseList = new ArrayList<>();

        //在数据库获取试题
        Exam exam = examService.get(id);
        //获取试卷里的问题
        List<Question> questions = exam.getQuestion();
        Integer onlyScore = 0;
        Integer manyScore = 0;
        Integer trueOrFalseScore = 0;
        //for循环遍历问题
        for (Question question : questions) {
            //拿到每个问题的类型
            Integer type = question.getType();
            //获取到问题中的选项
            if (type == 1) {
                onlyList.add(question);
                onlyScore += question.getScore();
            } else if (type == 2) {
                manyList.add(question);
                manyScore += question.getScore();
            } else {
                trueOrFalseList.add(question);
                trueOrFalseScore += question.getScore();
            }
        }
        model.addAttribute("onlyScore", onlyScore);
        model.addAttribute("manyScore", manyScore);
        model.addAttribute("trueOrFalseScore", trueOrFalseScore);

        model.addAttribute("onlyList", onlyList);
        model.addAttribute("manyList", manyList);
        model.addAttribute("trueOrFalseList", trueOrFalseList);
        model.addAttribute("exam", exam);
        return "exam/preview";
    }


    @RequiredPermission(name = "试卷新增页面", expression = "exam:input")
    @RequestMapping("/input")
    public String input() {

        return "exam/input";
    }


    //新增或者修改
    @RequiredPermission(name = "试卷新增", expression = "exam:save")
    @RequestMapping("/save")
    public String saveOrUpdate(Exam exam, ExamQueryObject qo) {

        examService.save(exam, qo);

        return "redirect:/exam/list";//让浏览器重新发送一次请求
    }
}
